﻿using CommunityToolkit.Mvvm.ComponentModel;
using HandyControl.Controls;
using OxyPlot;
using OxyPlot.Series;
using System;
using System.Collections.Generic;
using System.Collections.ObjectModel;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Documents;

namespace HardToolApp.Model
{
    public class ADCModel: Peripheral
    {
        public class ADCChannel : ObservableObject
        {
            private UInt16 _ch;
            public UInt16 ch { get { return _ch; } set { SetProperty(ref _ch, value);} }
            private bool? _chShow = true;
            public bool? chShow { get { return _chShow; } set { SetProperty(ref _chShow, value); chSeries.IsVisible = _chShow == true; } }
            LineSeries _chSeries;
            public LineSeries chSeries { get { return _chSeries; } set { SetProperty(ref _chSeries, value); } }
            public ushort[] chArr { get; set; } = new ushort[200];
            private ObservableCollection<int> _chList = new ObservableCollection<int>();
            public ObservableCollection<int> chList { get { return _chList; } set { SetProperty(ref _chList, value); } }
            public ADCChannel(string name)
            {
                chSeries = new LineSeries { Title = name, MarkerType = OxyPlot.MarkerType.Square };
                
            }

            private void ArrChange(ushort data)
            {
                ushort[] arr = new ushort[chArr.Length];
                List<DataPoint> ls = new List<DataPoint>();
                chSeries.Points.Clear();
                arr[0] = data;
                ls.Add(new DataPoint(0,data));
                for (int i = 0; i < chArr.Length - 1; i++)
                {
                    arr[i + 1] = chArr[i];
                    ls.Add(new DataPoint(i+1, arr[i + 1]));
                }
                chSeries.Points.AddRange(ls);

                chArr = arr;
            }
            public void Add(ushort data)
            {
                ch = data;
                ArrChange(data);
                chList.Add(ch);
                
            }
        }

        private ADCChannel _ch1;
        public ADCChannel ch1 { get { return _ch1; } set { SetProperty(ref _ch1, value); } }
        private ADCChannel _ch2;
        public ADCChannel ch2 { get { return _ch2; } set { SetProperty(ref _ch2, value); } }
        private ADCChannel _ch3;
        public ADCChannel ch3 { get { return _ch3; } set { SetProperty(ref _ch3, value); } }
        private ADCChannel _ch4;
        public ADCChannel ch4 { get { return _ch4; } set { SetProperty(ref _ch4, value); } }

        private PlotModel plot = new PlotModel();
        public PlotModel Plot { get { return plot; } set { SetProperty(ref plot, value); } }
        
        public ADCModel() : base(CommCmd.COMM_ADC)
        {
            ch1 = new ADCChannel("CH1");
            Plot.Series.Add(ch1.chSeries);
            ch2 = new ADCChannel("CH2");
            Plot.Series.Add(ch2.chSeries);
            ch3 = new ADCChannel("CH3");
            Plot.Series.Add(ch3.chSeries);
            ch4 = new ADCChannel("CH4");
            Plot.Series.Add(ch4.chSeries);
            OxyPlot.Axes.Axis xa = new OxyPlot.Axes.LinearAxis();
            xa.AbsoluteMinimum = 0;
            xa.AbsoluteMaximum = 200;
            xa.Position = OxyPlot.Axes.AxisPosition.Bottom;
            //xa.IsAxisVisible = false;
            Plot.Axes.Add(xa);

            OxyPlot.Axes.Axis ya = new OxyPlot.Axes.LinearAxis();
            ya.AbsoluteMinimum = 0;
            ya.AbsoluteMaximum = 5000;
            ya.Position = OxyPlot.Axes.AxisPosition.Left;
            Plot.Axes.Add(ya);
            Plot.ClipTitle = true;
        }
       
    }
}
